package com.yumei.trader.gym.client;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.pm.ApplicationInfo;
import android.provider.Settings;


import com.appsflyer.internal.AFa1aSDK;
import com.yumei.trader.gym.BuildConfig;
import com.yumei.trader.gym.dbase.DBaseConst;
import com.yumei.trader.gym.dbase.DBaseManager;
import com.yumei.trader.gym.util.StringUtils;
import com.yumei.trader.gym.view.IColor;

public class TGApplication extends Application {

    //本地数据存储
    private DBaseManager dbMrg;

    //通用用户Id
    private String userId;

    public static boolean vipUser = true;

    public boolean isConnectGoogle = false;

    private static TGApplication INSTANCE;

    public TGApplication() {
        super();
        INSTANCE = this;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initAF();

        //初始化系统数据
        initData();

        //初始化第三方SDK
        initSdk();

    }

    @SuppressLint("HardwareIds")
    private void initAF() {
        AFa1aSDK.getInstance().setAndroidIdData(
                Settings.Secure.getString(
                        getContentResolver(),
                        Settings.Secure.ANDROID_ID
                ));
        AFa1aSDK.getInstance().init(BuildConfig.AF_KEY, null, this);
        AFa1aSDK.getInstance().start(this);
    }


    private void initData() {

        CommonConst.IS_DEBUG = isApkInDebug();

        dbMrg = DBaseManager.getInstance(this);

        userId = dbMrg.getValue(DBaseConst.User_Id);

        if (StringUtils.isNull(userId)){
            userId = System.currentTimeMillis() + "";
            dbMrg.addParam(DBaseConst.User_Id, userId);
            dbMrg.save();
        }

        IColor.RED = 0xFF15CC63;
        IColor.GREEN = 0xFFE8533F;
        IColor.GREEN2 = 0xFFE8533F;
    }

    private void initSdk() {


    }

    public DBaseManager getDBMrg(){
        return dbMrg;
    }

    public String getUserId(){
        return userId;
    }

    public static TGApplication getInstance() {
        return INSTANCE;
    }

    public boolean isApkInDebug() {
        try {
            ApplicationInfo info = this.getApplicationInfo();
            return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
        } catch (Exception e) {
            return false;
        }
    }
}
